﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace huaguwang.hgmanage.Securities
{
    public partial class securities_E : System.Web.UI.Page
    {
        M_T_SecurCompany _model = new M_T_SecurCompany();
        D_T_SecurCompany _dal = new D_T_SecurCompany();
        private void SetButton(int i, Button but)
        {
            switch (i)
            {
                case 1:
                    but.CausesValidation = false;
                    break;
                default:
                    break;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                this.WebUserToolBar1.MainInit(
                      UserConst.Exp_GetUser,
                      UserConst.Exp_GetButtonList,
                      SetButton,
                     UserConst.Exp_ToolBarErr
                  );
                if (!IsPostBack)
                {
                    var id = Request.Params["ID"].Exp_IntTryParse();
                    ViewState.Add("id", id);
                    if (id > 0)
                    {
                        var model = _dal.GetModel(id);//获得电子书实体
                        if (model == null)
                        {
                            this.Exp_PageSkyAlterReplace("securities.aspx", WebUserToolBar1.EditSKey, "找不到相关资料");
                        }
                        else
                        {
                            //绑定信息
                            BindData(model);
                        }
                    }
                }
            });
        }

        /// <summary>
        /// 修改绑定数据
        /// </summary>
        private void BindData(M_T_SecurCompany model)
        {
            this.txtCompanyTitle.Text = model.SecurCompany.Exp_Trim();
            this.txtComm.Text = model.Commission.Exp_Trim();
            //图片显示
            FLWUpLoad1.SetIMG(model.Pic);
            this.txtContact.Text = model.Contact.Exp_Trim();
            this.txtTel.Text = model.Tel.Exp_Trim();
            //置顶为1，不置顶为0
            this.rdoListTopis.Text = model.Topis ? "1" : "0";
            //推荐为1，不推荐为0
            this.rblRem.Text = model.RemSign ? "1" : "0";
            this.txtSEOtitle.Text = model.WebTitle.Exp_Trim();
            this.txtSEOkey.Text = model.WebKey.Exp_Trim();
            this.txtSEOdec.Text = model.WebSummary.Exp_Trim();
            this.txtContent.Value = model.Explain.Exp_Trim();
            //省市区
            this.regAdress.Value = model.Provinces+","+model.City+","+model.County;
            //详细地址
            this.txtAdress.Text = model.Adress.Exp_Trim();
        }

        private M_T_SecurCompany GetModel()
        {
            AdminPublic.Try(() =>
            {
                _model.id = ViewState["id"].Exp_IntTryParse();
                _model.SecurCompany = this.txtCompanyTitle.Text.Exp_Trim();
                _model.Contact = this.txtContact.Text.Exp_Trim();
                _model.Explain = this.txtContent.Value.Exp_Trim();
                _model.Tel = this.txtTel.Text.Exp_Trim();
                _model.AddDate = DateTime.Now;
                _model.Commission = this.txtComm.Text.Exp_IntTryParse();
                //置顶
                _model.Topis = this.rdoListTopis.Text == "0" ? false : true;
                //推荐
                _model.RemSign = this.rblRem.Text == "0" ? false : true;

                _model.WebKey = this.txtSEOkey.Text != "" ? this.txtSEOkey.Text.Exp_Trim() : this.txtCompanyTitle.Text.Exp_Trim();
                _model.WebSummary = this.txtSEOdec.Text != "" ? this.txtSEOdec.Text.Exp_Trim() : this.txtCompanyTitle.Text.Exp_Trim();
                _model.WebTitle = this.txtSEOtitle.Text != "" ? this.txtSEOtitle.Text.Exp_Trim() : this.txtCompanyTitle.Text.Exp_Trim();
                _model.ColorKey = this.txtColorKey.Text != "" ? this.txtColorKey.Text.Exp_Trim() : this.txtCompanyTitle.Text.Exp_Trim();
                _model.ColorTitle = this.txtColorTitle.Text != "" ? this.txtColorTitle.Text.Exp_Trim() : this.txtCompanyTitle.Text.Exp_Trim();
                _model.InteriorKey = this.txtInKey.Text != "" ? this.txtInKey.Text.Exp_Trim() : this.txtCompanyTitle.Text.Exp_Trim();
                //省、市、区
                _model.Provinces = this.regAdress.Value.Exp_Trim().Exp_Split(',', 0);
                _model.City = this.regAdress.Value.Exp_Trim().Exp_Split(',', 1);
                _model.County = this.regAdress.Value.Exp_Trim().Exp_Split(',', 2);
                //获取操作用户ID
                _model.UserID = UserConst.Exp_GetUser(this).id;        
               //详细地址
                _model.Adress = this.txtAdress.Text.Exp_Trim();
            });
            return _model;
        }
        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button0(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                var model = GetModel();//证券公司实体
                //操作日志类型
                Log_Type ltype = new Log_Type(); 
                var iRet = 0;
                if (model.id > 0)
                {
                    //如果要保存图片,请注意此处的用法
                    var PicTemp = _dal.GetModelFlag(model.id);
                    var fileName = this.FLWUpLoad1.UpLoad();
                    if (!fileName)//上传失败
                    {
                        model.Pic = PicTemp.Pic;
                    }
                    else
                    {
                        model.Pic = FLWUpLoad1.FilePath;
                    }
                    var tempModel = _dal.GetModel(model.id);
                    if (tempModel == null || !tempModel.Flag)
                    {
                        this.Exp_PageAlter("当前信息不存在,可能已经被其他人删除!");
                        return;
                    }
                    iRet = _dal.Update(model);
                    ltype = Log_Type.add;
                }
                else
                {
                    //新增
                    var fileName = this.FLWUpLoad1.UpLoad();
                    if (!fileName)
                    {
                        model.Pic = "";
                    }
                    else
                    {
                        model.Pic = FLWUpLoad1.FilePath;
                    }
                    iRet = _dal.Add(model);
                    ltype = Log_Type.update;
                }
                if (iRet > 0)
                {
                    // 写日志
                    this.Exp_PageWriteLog(ltype, "证券公司信息", iRet);
                    var SKey = this.WebUserToolBar1.EditSKey;
                    this.Exp_PageAlterHref("信息操作成功！", "securities.aspx?" + SKey);
                }
                else
                {
                    this.Exp_PageAlter("操作失败");
                }
            });
        }

        /// <summary>
        /// 返回按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button1(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                var sKey = this.WebUserToolBar1.EditSKey;
                Response.Redirect("securities.aspx?" + sKey);
            });
        }
    }
}
